<?php !defined( 'SUNCMS' ) && exit( 'No direct script access allowed'  );

/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

/**转义函数**/
function new_addslashes( $string )
{
	if( is_array( $string ) )
	{
		foreach( $string as $key => $val )
		{
			$string[$key] = new_addslashes( $val );
		}
	}
	else
	{
		$string = addslashes( $string );
		$string = preg_replace('/union(\s*(\/\*.*\*\/)?\s*)+select/i', 'union &nbsp; select', $string);
	}
	return $string;
}

/**除空格函数**/
function strim( $string )
{
	if( is_array( $string ) )
	{
		foreach( $string as $key => $val )
		{
			$string[$key] = strim( $val );
		}
	}
	else
	{
		$string = trim( $string );
	}
	return $string;
}

/**字符串截取**/
function my_substr( $str, $length, $suffix = false, $charser = 'utf-8', $start = 0 )
{
	if( function_exists( 'mb_substr' ) ) 
	{
		$more = ( mb_strlen( $str ) > $length ) ? true : false;
		$text = mb_substr( $str, $start, $length, $charser );
		if( $suffix && $more ) $text.=" ...";
	} 
	elseif( function_exists( 'iconv_substr' ) ) 
	{
		$more = ( iconv_strlen( $str ) > $length ) ? true : false;
		$text = iconv_substr( $str, $start, $length, $charser );
		if( $suffix && $more ) $text.=" ...";
	} 
	else 
	{
		$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
		$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
		preg_match_all( $re[$charser], $str, $match );
		$text = join( "", array_slice( $match[0], $start, $length ) );
		if ( count( $match[0]) > $length ) 
		{
			$more = true;
			$text = join( "", array_slice( $match[0], 0, $length ) ); 
		} 
		else 
		{
			$more = false;
			$text = join( "", array_slice( $match[0], 0, $length ) ); 
		}
		if( $suffix && $more ) $text.=" ...";
	}
	return $text;
}

/**字符串长度**/
function my_strlen( $str, $charset='utf-8' )
{
	if( function_exists( 'mb_substr' ) )
	{
		$len = mb_strlen( $str, $charset );
		return $len;
	}
	elseif( function_exists( 'iconv_substr' ) ) 
	{
		$len = iconv_strlen( $str, $charset );
		return $len;
	} 
	else 
	{
		$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
		$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
		preg_match_all( $re[$charset], $str, $match );
		$len = count( $match[0] );
		return $len;
	}
}


function escape( $string )
{
	if( is_array( $string ) )
	{
		foreach( $string as $key => $val )
		{
			$string[$key] = escape( $val );
		}
	}
	else
	{
		$string = preg_replace( '/javascript/i', 'java script',$string );
		$string = preg_replace( '/<iframe ([^<>]+)>/i', '&lt;iframe \\1>',$string );
		$string = str_replace( "\t", "   &nbsp;  &nbsp;", $string );
		$string = str_replace( "   ", " &nbsp; ", $string );
	}
	return $string;
}

function new_stripslashes( $string )
{
	if( is_array( $string ) )
	{
		foreach( $string as $key => $val )
		{
			$string[$key] = new_stripslashes( $val );
		}
	}
	else
	{
		$string =  stripslashes( $string );
	}
	return $string;
}

/**获取cookie**/
function get_cookie( $name, $child = '' )
{
	$new_name = get_cookie_pre().$name;
	if( isset( $_COOKIE[$new_name] ) )
	{
		$cookie = unserialize( authcode( $_COOKIE[$new_name] ) );
		$cookie['update'] && ( ( $cookie['last_activity'] + 300 ) < TIME ) && set_cookie( $name, $cookie , $cookie['update']);
		return ( $child && isset( $cookie[$child] ) )?$cookie[$child]:$cookie;
	}
	else 
	{
		return false;
	}
}

/**设cookie**/
function set_cookie( $name, $data, $update = 0 )
{
	$cookie_data = $data;
	$cookie_data['last_activity'] = TIME;
	$cookie_data['update'] = $update;
	$cookie_data = authcode( serialize( $cookie_data ), 'ENCODE' );
	$data['time'] = empty( $data['time'] )?31104000:$data['time'];
	$name = get_cookie_pre().$name;
	return setcookie( $name, $cookie_data, TIME + $data['time'], COOKIE_PATH, COOKIE_DOMAIN, $_SERVER['SERVER_PORT'] == 443 ? 1 : 0 );
}

/**销毁cookie**/
function unset_cookie( $name )
{
	$pre = get_cookie_pre();
	if( is_array( $name ) )
	{
		foreach( $name as $v )
		{
			unset_cookie( $v );
		}
	}
	else 
	{
		setcookie( $pre.$name, '', TIME-31536000, COOKIE_PATH );
	}
}

/**获取cookie前缀**/
function get_cookie_pre()
{
	static $pre = null;
	if( !isset( $pre ) )
	{
		$pre = COOKIE_PRE == ''?substr( md5( ENCRYPT_KEY ), 0, 5 ).'_':COOKIE_PRE;
	}
	return $pre;
}


/**获取GET，POST的值**/
function get_post( $str )
{
	if( !isset( $_POST[$str] ) && !isset( $_GET[$str] ) ){
		return '';
	}else{
		return ( isset( $_POST[$str] )  )?$_POST[$str]:$_GET[$str];
	}
}

/**获取文件后缀**/
function get_fileext( $file, $comp = '.' )
{
	return strtolower( trim( substr( strrchr( $file, $comp ), 1 ) ) );
}

/**过滤非法词**/
function filter_word( $data = '' )
{
	global $SUNCMS;
	$filter_word = strim( $SUNCMS['filter_word'] );
	if( !$filter_word || ( !$data && !$_GET && !$_POST ) ) return false;
	$filter_word = array_filter( array_map( 'strim', explode( "|", $filter_word ) ) );
    if( !$filter_word ) return false;
	$pattern = str_replace( '\*', '.*', implode( '|', array_map( 'preg_quote', $filter_word) ) );
	$data = array2string( array_merge( $_GET, $_POST ) );
	if( $pattern && preg_match( "/($pattern)/", $data, $m ) )
	{
		$pattern_word = $m[0];
		define( 'ILLEGAL_WORD', $pattern_word );
		unset( $m[0] );
		$word = implode( ' ', $m );
		$logdata = array( TIME, IP, $word, $pattern_word );
		$logfile = SUNCMS.'datas/filterlog/'.date('Ym', TIME).'.csv';
		$fp = fopen( $logfile, 'a' );
		fputcsv( $fp, $logdata );
		fclose( $fp );
		return true;
	}
	return false;
}

/**记录系统日志**/
function systemlog( $log )
{
	$logdata = array( TIME, IP, $log );
	$logfile = SUNCMS.'datas/systemlog/'.date('Ym', TIME).'.csv';
	$fp = fopen( $logfile, 'a' );
	fputcsv( $fp, $logdata );
	fclose( $fp );
}

/**记录访问日志**/
function visitlog()
{
	
}

/**写csv文件**/
if( !function_exists('fputcsv') )
{
	function fputcsv( &$fp, $array, $delimiter = ',', $enclosure = '"' )
	{
		$data = $enclosure.implode( $enclosure.$delimiter.$enclosure, $array ).$enclosure."\n";
		return fwrite( $fp, $data );
	}
}

/**字符串转数组**/
function string2array( $data )
{
	if( $data == '' ) return array();
	eval( "\$array = $data;" );
	return $array;
}

/**数组转字符串**/
function array2string( $data, $isformdata = 1 )
{
	if( $data == '' ) return '';
	if( $isformdata ) $data = new_stripslashes( $data );
	return new_addslashes( var_export( $data, true ) );
}

/**显示消息**/
function show_msg( $msg, $url = 'goback', $direct = 0, $t = 5 )
{
    if( $direct )
    {
        ob_clean();
        header( "Refresh: $t; url=".$url );
    }
	include display( 'show_msg' );
	exit;
}

/**显示错误**/
function show_error( $error )
{
	systemlog( $error );
	include display( 'show_error' );
	exit;
}

/**获取IP**/
function get_ip() 
{  
	$ip = '0.0.0.0'; 
	if( getenv( 'HTTP_CLIENT_IP' ) && strcasecmp( getenv( 'HTTP_CLIENT_IP' ), 'unknown' ) )
	{
		$ip = getenv( 'HTTP_CLIENT_IP' );
	}
	elseif( getenv( 'HTTP_X_FORWARDED_FOR' ) && strcasecmp( getenv( 'HTTP_X_FORWARDED_FOR' ), 'unknown' ) )
	{
		$ip = getenv( 'HTTP_X_FORWARDED_FOR' );
	}
	elseif( getenv( 'REMOTE_ADDR' ) && strcasecmp( getenv( 'REMOTE_ADDR' ), 'unknown' ) )
	{
		$ip = getenv('REMOTE_ADDR');
	}
	elseif( isset( $_SERVER['REMOTE_ADDR'] ) && $_SERVER['REMOTE_ADDR'] && strcasecmp( $_SERVER['REMOTE_ADDR'], 'unknown' ) )
	{
		$ip = $_SERVER['REMOTE_ADDR'];
	}
	preg_match( "/[\d\.]{7,15}/", $ip, $ipmatches );
	return $ipmatches[0] ? $ipmatches[0] : 'unknown';
}

/**产生随机字符**/
function random( $length, $numeric = 0 )
{
	PHP_VERSION < '4.2.0' ? mt_srand( ( double )microtime() * 1000000 ) : mt_srand();
	$seed = base_convert( md5( print_r( $_SERVER, 1 ).microtime() ), 16, $numeric ? 10 : 35 );
	$seed = $numeric ? ( str_replace( '0', '', $seed ).'012340567890' ) : ( $seed.'zZ'.strtoupper( $seed ) );
	$hash = '';
	$max = strlen( $seed ) - 1;
	for( $i = 0; $i < $length; $i++ ) 
	{
		$hash .= $seed[mt_rand( 0, $max )];
	}
	return $hash;
}

/**读取文件**/
if( !function_exists('file_get_contents') ) 
{
	function file_get_contents( $filename )
	{
		@$fp = fopen($filename, 'r');
		@$content = fread($fp, filesize($filename));
		@fclose($fp);
		return $content;
	}
}

/**写文件**/
if( !function_exists('file_put_contents') )
{
	function file_put_contents( $file, $data, $append = '' )
	{
		$mode = $append == '' ? 'wb' : 'ab';
		$fp = @fopen($file, $mode) or show_error( "Can not open file $file !" );
		flock($fp, LOCK_EX);
		$result = @fwrite($fp, $data);
		flock($fp, LOCK_UN);
		@fclose($fp);
		return $result;
	}
}

/**转json**/
if( !function_exists('json_encode') )
{
	function json_encode($string)
	{
		require_once SUN_CLASS.'json.php';
		$json = new json();
		return $json->encode($string);
	}
}

/**解json**/
if( !function_exists('json_decode') )
{
	function json_decode( $string, $type = 1 )
	{
		require_once SUN_CLASS.'json.php';
		$json = new json();
		return $json->decode( $string, $type );
	}
}

/**编码转换**/
if( !function_exists('iconv') )
{
	function iconv( $in_charset, $out_charset, $str )
	{
		if( function_exists( 'mb_convert_encoding' ) )
		{
			return mb_convert_encoding( $str, $out_charset, $in_charset );
		}
		else
		{
			require_once SUN_FUNC.'iconv.php';
			$in_charset = strtoupper( $in_charset );
			$out_charset = strtoupper( $out_charset );
			if( $in_charset == 'UTF-8' && ( $out_charset == 'GBK' || $out_charset == 'GB2312' ) )
			{
				return utf8_to_gbk($str);
			}
			if( ( $in_charset == 'GBK' || $in_charset == 'GB2312' ) && $out_charset == 'UTF-8' )
			{
				return gbk_to_utf8($str);
			}
			return $str;
		}
	}
}

/**哈希加密**/
if( !function_exists('sha1') )
{
	function sha1($str)
	{
		if ( !function_exists( 'mhash' ) )
		{
			require_once 'Sha1.class.php';
			$sha1 = new Sha1();
			return $sha1->generate($str);
		}
		else
		{
			return bin2hex( mhash( MHASH_SHA1, $str ) );
		}
	}
}

/**读取缓存**/
function cache_read( $file, $include = 'i' )
{
	$cachefile = SUN_CACHE.$file.'.php';
	if( !file_exists( $cachefile ) )
	{
		return array();
	}
	else
	{
		return $include == 'i' ? include $cachefile : file_get_contents( $cachefile );
	}
}

/**限制IP**/
function ban_ip( $ip )
{
	return false;
}

/**计算程序运行时间**/
function runtime()
{
	$stime = explode( ' ', TIME_START );
	$etime = explode( ' ', microtime() );
	return number_format( ( $etime[1] + $etime[0] - $stime[1] - $stime[0] ), 6 );
}

/**判断是否开启gzip**/
function gzip()
{
	return ob_get_mode()?'enabled':'disabled';
}


/**页面跳转**/
function go_url( $url )
{
  $html = "<script>";
  $html .= "window.location.href='". $url ."'";
  $html .= "</script>";
  echo $html;
}

/**页面刷新**/
function reload()
{
  $html = "<script>";
  $html .= "window.location.reload();";
  $html .= "</script>";
  echo $html;
}

/**后退**/
function go_back()
{
  $html = "<script>";
  $html .= "history.back(-1);";
  $html .= "</script>";
  echo $html;
}

/**弹出框**/
function alert( $str )
{
  $html = "<script>";
  $html .= "alert('$str');";
  $html .= "</script>";
  echo $html;
}



/*****************************************Copyright (c) UCHome********************************************/

/**加解密**/
function authcode($string, $operation = 'DECODE', $expiry = 0) 
{
	$operation == 'DECODE' && $string = str_replace( '-', '+', str_replace( '_', '/', $string ) );
	$ckey_length = 4;	// 随机密钥长度 取值 0-32;
	// 加入随机密钥，可以令密文无任何规律，即便是原文和密钥完全相同，加密结果也会每次不同，增大破解难度。
	// 取值越大，密文变动规律越大，密文变化 = 16 的 $ckey_length 次方
	// 当此值为 0 时，则不产生随机密钥

	$key 	= md5( ENCRYPT_KEY );
	$keya 	= md5(substr($key, 0, 16));
	$keyb 	= md5(substr($key, 16, 16));
	$keyc 	= $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';

	$cryptkey = $keya.md5($keya.$keyc);
	$key_length = strlen($cryptkey);

	$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
	$string_length = strlen($string);

	$result = '';
	$box = range(0, 255);

	$rndkey = array();
	for($i = 0; $i <= 255; $i++) {
		$rndkey[$i] = ord($cryptkey[$i % $key_length]);
	}

	for($j = $i = 0; $i < 256; $i++) {
		$j = ($j + $box[$i] + $rndkey[$i]) % 256;
		$tmp = $box[$i];
		$box[$i] = $box[$j];
		$box[$j] = $tmp;
	}

	for($a = $j = $i = 0; $i < $string_length; $i++) {
		$a = ($a + 1) % 256;
		$j = ($j + $box[$a]) % 256;
		$tmp = $box[$a];
		$box[$a] = $box[$j];
		$box[$j] = $tmp;
		$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
	}

	if($operation == 'DECODE') {
		if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
			return substr($result, 26);
		} else {
			return '';
		}
	} else {
		return str_replace( '+', '-', str_replace( '/', '_', $keyc.str_replace('=', '', base64_encode($result)) ) );
	}
}


/*****************************************Copyright (c) PHPWind********************************************/

function ob_contents($output)
{
	ob_end_clean();
	$http_accept_encoding = $_SERVER['HTTP_ACCEPT_ENCODING'];
	if ( !headers_sent() && $http_accept_encoding && n_output_zip() != 'ob_gzhandler' && GZIP )
	{
		$encoding = '';
		if ( strpos( ' '.$http_accept_encoding, 'gzip' ) !== false ) {
			$encoding = 'gzip';
		} 
		elseif( strpos(' '.$http_accept_encoding, 'x-gzip' ) !== false )
		{
			$encoding = 'x-gzip';
		}
		if ( $encoding && function_exists( 'crc32' ) && function_exists( 'gzcompress' ) )
		{
			header( 'Content-Encoding: '.$encoding );
			$outputlen  = strlen( $output );
			$outputzip  = "\x1f\x8b\x08\x00\x00\x00\x00\x00";
			$outputzip .= substr( gzcompress( $output, 9 ), 0, -4 );
			$outputzip .= @pack( 'V', crc32( $output ) );
			$output = $outputzip.@pack( 'V', $outputlen );
		}
		else 
		{
			my_ob_start();
		}
	}
	else 
	{
		my_ob_start();
	}
	return $output;
}

function my_ob_start()
{
	ob_get_mode() == 1 ? ob_start( 'ob_gzhandler' ) : ob_start();
}

function ob_get_mode()
{
	static $mode = null;
	if ( $mode !== null )
	{
		return $mode;
	}
	$mode = 0;
	if ( GZIP && function_exists( 'ob_gzhandler' ) && n_output_zip() != 'ob_gzhandler' && ( !function_exists( 'ob_get_level' ) || ob_get_level() < 1 ) )
	{
		$mode = 1;
	}
	return $mode;
}

function n_flush()
{
	if ( n_output_zip() == 'ob_gzhandler' )
	{
		return;
	}
	if ( php_sapi_name() != 'apache2handler' && php_sapi_name() != 'apache2filter' ) 
	{
		flush();
	}
	if ( function_exists('ob_get_status') && ob_get_status() && function_exists('ob_flush') && !ObGetMode( $GLOBALS['db_obstart'] ) ) 
	{
		@ob_flush();
	}
}

function n_output_zip()
{
	static $output_handler = null;
	if ($output_handler === null) 
	{
		if ( @ini_get( 'zlib.output_compression' ) ) 
		{
			$output_handler = 'ob_gzhandler';
		}
		else
		{
			$output_handler = @ini_get( 'output_handler' );
		}
	}
	return $output_handler;
}

/**上传**/
function uploads( $save_path )
{
	global $action, $db, $SUNCMS, $user_id, $watermark, $thumb, $thumb_width, $thumb_height, $file_save_name;
	
	$error = $file_save_full_name = '';
	
	/**允许上传类型**/
	$type_array = explode( '|', $SUNCMS['upload_allow_type'] );
	
	/**最大文件大小**/
	$max_size = $SUNCMS['upload_allow_size'];

	/**存储地址**/
	$path = $action;
	$new_action = $action == 'thumb'?'images':$action;
	$relative_path = 'uploads/'.$action;
	$save_path = SUNCMS.'uploads/'.$save_path.'/';

	/**检查目录**/
	if ( @is_dir( $save_path ) === false ) 
	{
		create_dir( $save_path );
	}

	/**检查目录写权限**/
	if ( @is_writable( $save_path ) === false )
	{
		@mkdir( $save_path, 0777 );
	}
	
	if ( !empty( $_FILES ) ) 
	{
		/**原文件名**/
		$file_name = strtolower( $_FILES['file']['name'] );
		/**服务器上临时文件名**/
		$tmp_name = $_FILES['file']['tmp_name'];
		/**文件大小**/
		$file_size = ceil( $_FILES['file']['size']/1024 );
		
		/**检查文件**/
		if ( !$file_name ) return array( 'error' => "上传文件不存在！", 'file_name' => '' );
		/**检查是否已上传**/
		if ( @is_uploaded_file($tmp_name) === false ) return array( 'error' => "临时文件可能不是上传文件！", 'file_name' => '' );
		/**检查文件大小**/
		if ( $file_size > $max_size ) return array( 'error' => "上传文件大小超过限制！", 'file_name' => '' );
		/**检查用户空间**/
		$member_sql = "SELECT `b`.`power`, `c`.`uploadsize` FROM `".DB_PRE."member` AS `a` 
								LEFT JOIN `".DB_PRE."membergroup` AS `b` ON `a`.`group_id` = `b`.`id` 
								LEFT JOIN `".DB_PRE."memberdata` AS `c` ON `c`.`uid` = $user_id 
								WHERE `a`.`id` = $user_id";
		$member_info = $db->query( $member_sql, true );
		if( $member_info['power']['uploadzone'] != 0 )
		{
			if( $member_info['uploadsize'] >= $SUNCMS['uploadsize'] || ( ( $member_info['uploadsize'] + $file_size ) > $SUNCMS['uploadsize'] ) )
			{
				return array( 'error' => "用户空间不足！", 'file_name' => '' );
			}
		}
		
		/**获得文件扩展名**/
		$file_ext = get_fileext( $file_name );
		
		/**检查扩展名**/
		if ( in_array( $file_ext, $type_array ) === false ) return array( 'error' => '不允许的文件格式！', 'file_name' => '' );
	
		/**文件保存名**/
		if( !empty( $file_save_name ) )
		{
			$sql_file_name = "SELECT `name` FROM `".DB_PRE."$new_action` WHERE `name` = '$file_save_name'";
			$result_name = $db->query( $sql_file_name, true );
			!empty( $result_name ) && $file_save_name = $file_save_name.'-'.random( 5 );
		}
		else 
		{
			$file_save_name = md5( $file_name.microtime(true) );
		}

		/**文件保存全名**/
		$file_save_full_name = $file_save_name.'.'.$file_ext;
		
		$save_path = $save_path . $file_save_full_name;
		/**移动文件**/
		if ( move_uploaded_file( $tmp_name, $save_path ) === false )
		{
			return array( 'error' => "上传文件失败！", 'file_name' => '' );
		}
		@chmod( $save_path, 0777 );

		if( $new_action == 'images' )
		{
			include( SUNCMS.'include/images.class.php' );
			$m_images = new Images();
			if( $action == 'images' )
			{
				if( $thumb ) 
				{
					if( $watermark )
					{
						$m_images->resize_images( $file_save_full_name, $thumb_width, $thumb_height, 'temp2' );
						$m_images->watermark_word( $file_save_full_name, $SUNCMS['watermark_word'], 'temp2', 'thumb' );
					}
					else
					{
						$m_images->resize_images( $file_save_full_name,  $thumb_width, $thumb_height, 'thumb' );
					}
					$relative_path = 'uploads/thumb';
					$path = 'thumb';
				}
				elseif( !$thumb && $watermark )
				{
					$m_images->watermark_img( $file_save_full_name, $SUNCMS['watermark_img'] );
				}
			}
			elseif( $action == 'thumb' ) 
			{
				if( $watermark )
				{
					$m_images->resize_images( $file_save_full_name, $thumb_width, $thumb_height, 'temp2' );
					$m_images->watermark_word( $file_save_full_name, $SUNCMS['watermark_word'], 'temp2', 'thumb' );
				}
				else 
				{
					$m_images->resize_images( $file_save_full_name, $thumb_width, $thumb_height );
				}
			}
		}
		
		if( $new_action == 'images' ) 
		{
			$upload_data = array( 'name' => $file_save_name, 'path' => $path, 'size' => $file_size, 'type' => $file_ext, 'thumb' => 0 );
		}
		elseif( $new_action == 'flash' ) 
		{
			$upload_data = array( 'name' => $file_save_name, 'path' => $path, 'size' => $file_size );
		}
		elseif( $new_action == 'media' ) 
		{
			$upload_data = array( 'name' => $file_save_name, 'path' => $path, 'size' => $file_size, 'type' => $file_ext );
		}
		elseif( $new_action == 'enclosure' )
		{
			$encrypt = get_post( 'encrypt' ); $encrypt = $encrypt?1:0;
			$upload_data = array( 'name' => $file_save_name, 'path' => $path, 'size' => $file_size, 'type' => $file_ext, 'encrypt' => $encrypt );
			$file_save_full_name = SITE_URL.'download.php?file='.authcode( $file_save_name, 'ENCODE' );
		}
		$db->insert( DB_PRE.$new_action, $upload_data );
		$db->update( DB_PRE.'memberdata', array( 'uploadsize' => $member_info['uploadsize'] + $file_size ), array( 'uid' => $user_id ) );
		
		$file_url = SITE_URL.$relative_path.'/'.$file_save_full_name;
		return array( 'error' => "", 'file_name' => ( $action == 'thumb' || $action == 'enclosure' )?$file_save_full_name:$file_url );
	}
	else
	{
		return array( 'error' => "上传文件不存在！", 'file_name' => '' );
	}
}

/**下载**/
function downloads( $filename, $data  )
{	
	if( $filename == '' || $data == '' )
	{
		return false;
	}
	//获取文件后缀
	$ext = get_fileext( $filename );
	//载入类型库
	@include( SUN_CONFIG.'mimes.php' );
	//设置默认类型
	if ( !isset( $mimes[$ext] ) )
	{
		$mime = 'application/octet-stream';
	}
	else
	{
		$mime = ( is_array( $mimes[$ext] ) ) ? $mimes[$ext][0] : $mimes[$ext];
	}
	if ( strstr( $_SERVER['HTTP_USER_AGENT'], "MSIE" ) )
	{
		header('Content-Type: "'.$mime.'"');
		header('Content-Disposition: attachment; filename="'.$filename.'"');
		header('Expires: 0');
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		header("Content-Transfer-Encoding: binary");
		header('Pragma: public');
		header("Content-Length: ".strlen( $data ));
	}
	else
	{
		header('Content-Type: "'.$mime.'"');
		header('Content-Disposition: attachment; filename="'.$filename.'"');
		header("Content-Transfer-Encoding: binary");
		header('Expires: 0');
		header('Pragma: no-cache');
		header("Content-Length: ".strlen( $data ));
	}
	exit( $data );
}


/**跳转提示函数**/
Function jump( $message = 'SunCMS提示：页面跳转中。。。', $url="", $t='16' ){
	ob_clean();
	header("Refresh: $t; url=".$url);
	echo   "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
			<title>页面跳转中。。。</title>
			<style type='text/css' rel='stylesheet'>
			*{padding:0;margin:0;text-decoration:none; word-wrap:break-word; outline:none; }
			body{ background:#FFF; text-align:center; }
			a{text-decoration:none;color:#737373;}
			a:hover{text-decoration:underline;}
			.container{text-align:center; }
			.infobox{width:600px; margin:140px auto 0; height:150px;text-align:center; border:4px solid #DEEFFA;background:#F2F9FD; zoom:1; }
			.infotitle1{ margin:30px 0 5px; color:#09C; font-size:14px; font-weight:700; }
			.marginbot{ margin:20px 0 15px; }
			.lightlink{ color:#666; text-decoration:underline;}
			</style></head><body><div class='container' id='cpcontainer'>
			<div class='infobox'>
			<h4 class='infotitle1'>".$message."</h4>
			<img src='".SITE_URL."images/system/loading.gif' class='marginbot' />
			<p class='marginbot'><a href='".$url."' class='lightlink'>如果您的浏览器没有跳转，请点击这里！</a></p>
			</div></div></BODY></HTML>";
	unset( $message, $url, $t);
	exit();
}